﻿using System;
using System.Diagnostics;
using BrnMall.Manager.Manager.Store;
using BrnMall.Manager.Manager.WeApp;
using BrnMall.Services;
using BrnMall.WeiXin.Sdk;
using System.Drawing;
using System.Drawing.Imaging;

namespace BrnMall.Business
{
    public static class WxQRCode
    {
        /// <summary>
        /// 获取小程序二维码
        /// </summary>
        /// <param name="storeId">店铺ID</param>
        /// <param name="path">包含的路径</param>
        /// <param name="weAppId">小程序配置ID</param>
        /// <returns></returns>
        public static string GetWxaCode(int storeId, string path, int weAppId = 0)
        {
            try
            {

                var storeInfo = StoreWxAppManager.GetStoreDefaultWxAppById(storeId, weAppId);
                var fileUrl = WeiXinSdk.GetWxaCode(storeInfo.WeAppId, storeInfo.WeAppSecret,
                    path);
                var name = Uploads.SaveUploadUserWxaCode(fileUrl);
                return name;

            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                Trace.TraceError(e.StackTrace);
            }
            return string.Empty;
        }
        /// <summary>
        /// 通过自动打包的获取二维码
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="path"></param>
        /// <param name="storeAppId"></param>
        /// <returns></returns>
        public static string GetWxaCodeByStoreAppId(int storeId, string path, int storeAppId)
        {
            try
            {
                StoreAppReleaseManager.RefreshAuthorizerToken(storeAppId);
                var auth = AppAuthorizationManager.GetAppAuthorizationByStoreAppId(storeAppId);
                var fileUrl = WeiXinSdk.GetWxaCodeByAccessToken(auth.Authorizer_Access_Token,
                    path);
                var name = Uploads.SaveUploadUserWxaCode(fileUrl);
                return name;

            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                Trace.TraceError(e.StackTrace);
            }
            return string.Empty;
        }


        #region 餐桌二维码合成

        /// <summary>
        /// 合成
        /// </summary>
        /// <param name="backgroundImage">背景图</param>
        /// <param name="coverageImage">图层</param>
        /// <param name="description">描述</param>
        public static void SynthesisDiningQrCode(string backgroundImage, string coverageImage, string description, string saveFilepath)
        {
            //创建一张空白图层
            Image bmp = new Bitmap(800, 1155);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                //设置高质量插值法
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                //设置高质量,低速度呈现平滑程度
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //清空画布并以透明背景色填充
                g.Clear(Color.White);
                g.DrawImage(Image.FromFile(backgroundImage), 0, 0);

                //裁剪图片二维码
                Bitmap cuteBmp = CuteImage(coverageImage, 0, 0, 320, 320);
                g.DrawImage(cuteBmp, new Rectangle(240, 120, 320, 320), new Rectangle(0, 0, 320, 320), GraphicsUnit.Pixel);
                //g.DrawRectangle(new Pen(Brushes.Black, 1), new Rectangle(240, 120, 320, 320));
                Font font = new Font("宋体", 24f, FontStyle.Bold);
                g.DrawString(description, font, Brushes.OrangeRed, (800 - g.MeasureString(description, font).Width) / 2, 550);
                bmp.Save(saveFilepath, ImageFormat.Jpeg);
            }
        }

        /// <summary>
        /// 裁剪图片
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="StartX"></param>
        /// <param name="StartY"></param>
        /// <param name="destWidth"></param>
        /// <param name="destHeight"></param>
        /// <returns></returns>
        static Bitmap CuteImage(string filepath, int StartX, int StartY, int destWidth, int destHeight)
        {
            Image image = Image.FromFile(filepath);
            int srcWidth = image.Width;
            int srcHeight = image.Height;
            if (StartX >= srcWidth || StartY >= srcHeight)
            {
                // 开始截取坐标过大时，结束处理   
                return null;
            }

            if (StartX + destWidth > srcWidth)
            {
                // 宽度过大时只截取到最大大小   
                destWidth = srcWidth - StartX;
            }

            if (StartY + destHeight > srcHeight)
            {
                // 高度过大时只截取到最大大小   
                destHeight = srcHeight - StartY;
            }

            try
            {
                // 根据目标图片的大小，实例化一个画布  
                Bitmap bmpOut = new Bitmap(destWidth, destHeight);
                // 实例化一个画笔  
                using (Graphics g = Graphics.FromImage(bmpOut))
                {
                    // 将原始图片画到目标画布上  
                    g.DrawImage(image, StartX, StartY, destWidth, destHeight);
                }

                return bmpOut;
            }
            catch
            {
                return null;
            }
        }
        #endregion
    }
}